/**
 * ConfigManager.java
 */
package model;

import java.beans.*;
import java.io.*;
import java.util.*;
import dataTypes.*;


/**
 * @author Michal Kolodziejski
 *
 */
public class ConfigManager {
	private String mainConfigFilePath = "TIN-P2P.config";
	private String sharedConfigFilePath = "sharedFiles.config";
	private String downloadConfigFilePath = "downloadFiles.config";
	
	public ConfigManager(){
	}


	/**
	 * Reads a configuration file for shared files and returns list of shared files. 
	 * @return list of shared files
	 */
	public List<dataTypes.File> getSharedFilesList(){
		List<dataTypes.File> l = new ArrayList<dataTypes.File>();
		try{
			java.io.File configFile = new java.io.File(sharedConfigFilePath);
			if(configFile.exists() && configFile.canRead()){
				XMLDecoder xml = new XMLDecoder(new BufferedInputStream(new FileInputStream(sharedConfigFilePath)));
				l = (ArrayList) xml.readObject();
				xml.close();
			}
			else{
				// create a new one
				this.saveSharedFilesList(l);
			}
		} catch (NoSuchElementException e){
			System.err.println("Error in getSharedFilesList: configuration file is broken! Reseting...");
			l.clear();
			this.saveSharedFilesList(l);
		} catch(Exception e){
			e.printStackTrace();
			System.exit(-1);
		}
		return l;
	}
	
	/**
	 * Saves into a configuration file the list of shared files.
	 * @param list a list of shared files
	 */
	public void saveSharedFilesList(List<dataTypes.File> list){
		try{
			XMLEncoder xml = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(sharedConfigFilePath)));
			xml.writeObject(list);
			xml.flush();
			xml.close();
		} catch(Exception e){
			e.printStackTrace();
			System.exit(-1);
		}
	}


	/**
	 * Reads a configuration file for download files and returns list of download files. 
	 * @return list of download files
	 */
	public List<ModelDownloadFile> getDownloadFilesList(){
		List<ModelDownloadFile> l = new ArrayList<ModelDownloadFile>();
		try{
			java.io.File configFile = new java.io.File(downloadConfigFilePath);
			if(configFile.exists() && configFile.canRead()){
				XMLDecoder xml = new XMLDecoder(new BufferedInputStream(new FileInputStream(downloadConfigFilePath)));
				l = (ArrayList) xml.readObject();
				xml.close();
			}
			else{
				// create a new one
				this.saveDownloadFilesList(l);
			}
		} catch (NoSuchElementException e){
			System.err.println("Error in getDownloadFilesList: configuration file is broken! Reseting...");
			l.clear();
			this.saveDownloadFilesList(l);
		} catch(Exception e){
			e.printStackTrace();
			System.exit(-1);
		}
		return l;
	}
	
	/**
	 * Saves into a configuration file the list of download files.
	 * @param list a list of download files
	 */
	public void saveDownloadFilesList(List<ModelDownloadFile> list){
		try{
			XMLEncoder xml = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(downloadConfigFilePath)));
			xml.writeObject(list);
			xml.flush();
			xml.close();
		} catch(Exception e){
			e.printStackTrace();
			System.exit(-1);
		}
	}


	
	
	public void getGlobalSettings(GlobalSettings globalSettings){
		Integer m1 = new Integer(0);
		Integer m2 = new Integer(0);
		try{
			java.io.File configFile = new java.io.File(mainConfigFilePath);
			if(configFile.exists() && configFile.canRead()){
				XMLDecoder xml = new XMLDecoder(new BufferedInputStream(new FileInputStream(mainConfigFilePath)));
				m1 = (Integer) xml.readObject();
				m2 = (Integer) xml.readObject();
				globalSettings.setMaxActiveDownloadsNumber(m1);
				globalSettings.setMaxConnectionsPerDownload(m2);
				xml.close();
			}
			else{
				// create a new one
				this.saveGlobalSettings(globalSettings);
			}
		} catch (NoSuchElementException e){
			System.err.println("Error in getGlobalSettings: configuration file is broken! Reseting...");
			this.saveGlobalSettings(globalSettings);
		} catch(Exception e){
			e.printStackTrace();
			System.exit(-1);
		}
	}
	
	
	public void saveGlobalSettings(GlobalSettings globalSettings){
		try{
			XMLEncoder xml = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(mainConfigFilePath)));
			xml.writeObject(globalSettings.getMaxActiveDownloadsNumber());
			xml.writeObject(globalSettings.getMaxConnectionsPerDownload());
			xml.flush();
			xml.close();
		} catch(Exception e){
			e.printStackTrace();
			System.exit(-1);
		}
	}
}
